import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		loginProvider: "weixin",
		userInfo: null,
		baseUserInfo: null,
		userInfoAll: null,
		deviceInfo: null,
		token: null,
		BasicDataLocal: null,
		zcartData: [],
		w_planId: null,
		w_planName: null,
		w_planIdS: null,
		w_planNameS: null,
		pid: null,
		w_drugId: null,
		w_type:null,
	},
	mutations: {
		w_setPlanId (state, param) {
			state.w_planId = param.id;
			state.w_planName = param.pandianname;			
		},
		w_setPlanIdS (state, param) {
			state.w_planIdS = param.id;
			state.w_planNameS = param.pandianname;
			state.w_type = param.ftype;
		},
		w_setdrugId (state, param) {
			state.w_drugId = param;
		},
		w_setPid (state, param) {
			state.pid = param;
			uni.setStorageSync('zuipid', JSON.stringify(param));
		},
		login(state, param) {
			state.hasLogin = true;
			let _cname = '';
			if(param.baseUserInfo.userThirdDtos && param.baseUserInfo.userThirdDtos.length){
				if(param.baseUserInfo.memberDto && param.baseUserInfo.memberDto.name)
					_cname = param.baseUserInfo.memberDto.name;
				else
					_cname = param.baseUserInfo.userThirdDtos[0].nickname;
			}else if(param.baseUserInfo.memberDto && param.baseUserInfo.memberDto.userId){
				if(param.baseUserInfo.memberDto.name){
					_cname = param.baseUserInfo.memberDto.name;
				}else{
					_cname = `游客${param.baseUserInfo.memberDto.tel}`;
				}
			}else{
				_cname = '游客';
			}
			param.baseUserInfo.memberDto.name = _cname;
			state.userInfoAll = param;
			state.userInfo = param.userInfo;
			state.deviceInfo = param.deviceInfo;
			state.token = param.token;
			this.commit('setBaseUserInfo', param.baseUserInfo);
			try {
				uni.setStorageSync('userInfoAll', JSON.stringify(state.userInfoAll));
			} catch (e) {
				// error
			}
		},
		logout(state) {
			state.hasLogin = false
		},
		setUserInfo(state, userinfo){
			state.userInfo = userinfo;
		},
		setBaseUserInfo(state, baseUserInfo){
			state.baseUserInfo = baseUserInfo;
		},
		setZcartData(state, zcartData){
			let xzcartData = zcartData || [];
			state.zcartData = xzcartData;
		},
		setBasicData (state, tempBasicData) {
			state.BasicDataLocal = tempBasicData;
			try {
				uni.setStorageSync('BasicDataLocal', JSON.stringify(state.BasicDataLocal));
			} catch (e) {
				// error
			}
		},
		toMainPage (state, loginType) {
			try {
				uni.setStorageSync('zuiloginType', loginType);
			} catch (e) {
				// error
			}
			uni.switchTab({
				url: '/pages/index/home'
			});
		}
	},
	actions: {
		
	}
})

export default store
